{{ define "/admin/article/add" }}

{{ template "public/header" }}

<script type="text/javascript" src="/static/lib/ueditor-plus-master/dist-min/ueditor.config.js"></script>
<script type="text/javascript" src="/static/lib/ueditor-plus-master/dist-min/ueditor.all.js"></script>
<style>
    ul li {
        float: left;
        list-style: none;
        padding-right: 10px;
    }
    img {
        width: 300px;
        height: 300px;
    }
</style>

<body>
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:left" onclick="location.reload()" title="刷新">
        <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>
    </a>
</div>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" method="post">
            <div class="layui-form-item">
                <label for="title" class="layui-form-label">
                    <span class="x-red">*</span>标题
                </label>
                <div class="layui-input-block">
                    <input type="text" id="title" name="title" required  lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="desc" class="layui-form-label">
                    <span class="x-red">*</span>摘要
                </label>
                <div class="layui-input-block">
                    <input type="text" id="desc" name="desc" required lay-verify="required" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label for="editor" class="layui-form-label">
                    <span class="x-red">*</span>内容
                </label>
                <div class="layui-input-block">
                    <div style="height: 350px; overflow-y: scroll">
                        <textarea name="content" id="editor" style="height: 300px"></textarea>
                    </div>
                </div>
            </div>

            <div class="layui-form-item" >
                <label class="layui-form-label">设置</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="is_open_comment" title="开启评论" value='1'>
                    <input type="checkbox" name="is_recommended" title="推荐" value='1'>
                    <input type="checkbox" name="is_top" title="置顶" value='1'>
                </div>
            </div>

<!--            <div class="layui-form-item">-->
<!--                <label class="layui-form-label">封面</label>-->
<!--                <div class="layui-input-block">-->
<!--                    <input type="file" name="file" value="" style="line-height: 38px;">-->
<!--                </div>-->
<!--            </div>-->

            <div class="layui-form-item">
                <label class="layui-form-label"><span class="x-red">*</span>分类</label>
                <div class="layui-input-block">
                    {{ range $k, $v := .category}}
                    <input type="checkbox" name="category_id" value="{{ $v.Id }}" title="{{ $v.Name }}">
                    {{ end }}
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">
                    添加
                </button>
            </div>
        </form>
    </div>
</div>
</body>
<script>
    layui.use(['form', 'layer'], function () {
        var form = layui.form
        var layer = layui.layer

        var ue = UE.getEditor('editor', {
            // ... 更多配置
        });

        //监听提交
        form.on('submit(add)', function (obj) {
            var categoryId = []
            $.each($('input[name=category_id]'), function (k, v) {
                if ($(this).prop('checked')) {
                    categoryId.push($(this).val())
                }
            })
            obj.field.category_id = categoryId
            $.ajax({
                type: "post",
                url: obj.form.action,
                data: obj.field,
                dataType: "json",
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 3000//2秒关闭（如果不配置，默认是3秒）
                        });
                    }
                }
            });
            return false;
        });
    });
</script>
</html>
{{ end }}